<?php
namespace Admin\Controller;

class SysconfigController extends AbstractController  {
	
	protected $From;
    private $configLabel = null;

	public function _initialize(){
		$this->Form = D('Sysconfig');
        $this->configLabel = [
            'smtp_server' => '发送主机',
            'smtp_username' => '发件邮箱',
            'smtp_password' => '发件授权码',
            'sms_access_key' => '短信access_key',
            'sms_access_secret' => '短信access_secret',
            'sms_assign' => '短信签名'
        ];
    }
    
    public function index(){
        $config = M('Sysconfig')->group('ConfigClass,ID')->order('ID asc')->select();
        $this->assign('config',$config);
        $this->assign('configLabel',$this->configLabel);
        $this->display();
    }
    public function edit(){
        if (IS_POST) {
            $db_sysconfig  = M('Sysconfig');
            $param = I('post.param');
            foreach ($param as $con){
                $where['ConfigClass'] = array('eq',$con['ConfigClass']);
                $where['ConfigName'] = array('eq',$con['ConfigName']);

                $cf = $db_sysconfig->where($where)->find();
                if($cf){
                    $data['ConfigValue'] = $con['ConfigValue'];
                    $db_sysconfig->where($where)->save($data);
                }else{
                    $data['ConfigValue'] = $con['ConfigValue'];
                    $data['ConfigClass'] = $con['ConfigClass'];
                    $data['ConfigName'] = $con['ConfigName'];
                    $db_sysconfig->add($data);
                }
            }
            $this->success("修改成功！");
        }
    }
	
	public function save($type = false, $id = 0){
		if($type != false){
			if($type == "edit"){
				
			}else if($type == "update"){
				$result = $this->Form->UpdataList();
			}
			if($result['state'] == 1 && !empty($result)){
				$this->success($result['msg']);
			}else if($result['state'] == 0 && !empty($result)){
				$this->error($result['msg']);
			}
		}else{
			$res = $this->Form->MyshowAll();
			$newres = array();
			foreach($res AS $k => $v){
				$newres[$v['ConfigName']] = $v['ConfigValue'];
			}
			$this->assign("editdata",$newres);
			$this->display();
		}
	}
	


}